package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.SealPositionAddDTO;
import org.jsola.contract.dto.SealPositionBatchDTO;
import org.jsola.contract.dto.SealPositionUpdateDTO;
import org.jsola.contract.query.SealPositionQuery;
import org.jsola.contract.service.ISealPositionService;
import org.jsola.contract.vo.SealPositionListVO;
import org.jsola.contract.vo.SealPositionVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 合同默认签章位置
 *
 * @author JanusMix
 */
@RestController("contractSealPositionController")
@RequestMapping(ContractConstants.API_PATH + "/v1/sealPositions")
public class SealPositionController implements ISealPositionControllerAPI {

    @Autowired
    private ISealPositionService sealPositionService;

    @Override
    @PostMapping
    public Result<SealPositionVO> save(@Valid @RequestBody SealPositionAddDTO sealPositionAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同默认签章位置权限校验

        return Result.success(
                sealPositionService.save(sealPositionAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping("saveOrUpdate")
    public Result<List<SealPositionVO>> saveOrUpdate(@Valid @RequestBody List<SealPositionAddDTO> sealPositionAddDTOList) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同默认签章位置权限校验

        return Result.success(
                sealPositionService.saveOrUpdate(sealPositionAddDTOList, tokenUser)
        );
    }

    @Override
    @PostMapping("saveOrUpdate/{templateId}")
    public Result<List<SealPositionVO>> saveOrUpdate(@PathVariable Long templateId, @Valid @RequestBody List<SealPositionAddDTO> sealPositionAddDTOList) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同默认签章位置权限校验

        return Result.success(
                sealPositionService.saveOrUpdate(templateId, sealPositionAddDTOList, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SealPositionUpdateDTO sealPositionUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改合同默认签章位置权限校验

        int count = sealPositionService.update(sealPositionUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody SealPositionBatchDTO sealPositionBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<SealPositionAddDTO> sealPositionAddDTOList = sealPositionBatchDTO.getSealPositionAddDTOList();
        if (!CollectionUtils.isEmpty(sealPositionAddDTOList)) {
            sealPositionService.batchSave(sealPositionAddDTOList,tokenUser);
        }
        // 修改
        List<SealPositionUpdateDTO> sealPositionUpdateDTOList = sealPositionBatchDTO.getSealPositionUpdateDTOList();
        if (!CollectionUtils.isEmpty(sealPositionUpdateDTOList)) {
            sealPositionUpdateDTOList.parallelStream().forEach(
                    sealPositionUpdateDTO -> {
                        sealPositionService.update(sealPositionUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<Long> deleteSealPositionIdList = sealPositionBatchDTO.getDeleteSealPositionIdList();
        if (!CollectionUtils.isEmpty(deleteSealPositionIdList)) {
            sealPositionService.deleteByIds(tokenUser,deleteSealPositionIdList.toArray(new Long[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{sealPositionId}")
    public Result<Integer> delete(@PathVariable Long sealPositionId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除合同默认签章位置权限校验

        int count = sealPositionService.deleteByIds(tokenUser, sealPositionId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] sealPositionIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同默认签章位置权限校验

        int count = sealPositionService.deleteByIds(tokenUser, sealPositionIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{sealPositionId}")
    public Result<SealPositionVO> findById(@PathVariable Long sealPositionId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同默认签章位置权限校验

        return Result.success(
            sealPositionService.selectById(sealPositionId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SealPositionListVO>> listAll(@Valid SealPositionQuery sealPositionQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同默认签章位置权限校验

        return Result.success(
            sealPositionService.select(sealPositionQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SealPositionListVO>> page(@Valid SealPositionQuery sealPositionQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            sealPositionService.selectPage(sealPositionQuery, tokenUser.getSiteId())
        );
    }


}

